/**
 * 功能描述
 */

define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	
    module.exports = {
    	saveAdjust: function(widget){
    		var adjustTypes = [];
    		var preGroupData = widget.get("tempGridRowData").nonMemberGroup;
    		var adjustGroupData = widget.get("form").getData();
    		var signUpRooms = widget.get("editgrid").getData();
    		var preRoomDatas = widget.signUpRoomDatas;
    		if(signUpRooms.length == 0){
    			Dialog.alert({
    				tip: "提示！",
    				content: "预约房型不能为空"
    			});
    			return false;
    		}
    		// 格式化预约房型数据
    		var flag = true;
    		if(signUpRooms.length != preRoomDatas.length){
    			adjustTypes.push("RoomTypes");
    			flag = false;
    		}
    		for(var i=0; i < signUpRooms.length; i++){
    			if(typeof signUpRooms[i].roomStatus != "string"){
    				signUpRooms[i].roomStatus = signUpRooms[i].roomStatus.key;
    			}
    			if(signUpRooms[i].roomType.pkRoomType){
    				signUpRooms[i].roomType = signUpRooms[i].roomType.pkRoomType;
    			}
    			if(flag){
    				var haveSame = true;
    				for(var j=0; j < preRoomDatas.length; j++){
    					// 房型一致,数量不一致
    					if(preRoomDatas[j].roomType.pkRoomType){
    						preRoomDatas[j].roomType = preRoomDatas[j].roomType.pkRoomType;
    	    			}
    					if(preRoomDatas[j].roomType == signUpRooms[i].roomType){
    						haveSame = false;
    						if(preRoomDatas[j].count != signUpRooms[i].count){
    							adjustTypes.push("RoomTypes");
    			    			flag = false;
    			    			break;
    						}
    					}
    				}
    				// 房型不一致
    				if(haveSame){
    					adjustTypes.push("RoomTypes");
    				}
    			}
    		}
    		var preCheckInDate = preGroupData.checkInDate,
	    		preCheckOutDate = preGroupData.checkOutDate,
	    		prePerNumber = preGroupData.perNumber,
	    		adjustPerNumber = adjustGroupData.perNumber,
	    		adjustCheckInDate = adjustGroupData.checkInDate,
	    		adjustCheckOutDate = adjustGroupData.checkOutDate;
    		// 根据日期判断调整类型是提前还是推迟
    		if(adjustCheckInDate < preCheckInDate){
    			adjustTypes.push("Advanced");
    		}else if(adjustCheckInDate > preCheckInDate){
    			adjustTypes.push("Delayed");
    		}else if(adjustCheckOutDate > preCheckOutDate){
    			adjustTypes.push("Delayed");
    		}else if(adjustCheckOutDate < preCheckOutDate){
    			adjustTypes.push("Advanced");
    		}
    		// 根据预约人数判断调整类型是否人员调整
    		if(prePerNumber != parseInt(adjustPerNumber)){
    			adjustTypes.push("Turnover")
    		}
    		if(adjustTypes.length == 0){
    			Dialog.alert({
    				tip: "提示！",
    				content: "未对该团进行调整！"
    			});
    			return false;
    		}
    		adjustGroupData.adjustTypes = adjustTypes;
    		adjustGroupData.isAdjustment = true;
    		adjustGroupData.adjustDate = moment().format("x");
    		var saveData = {
    				nonMemberGroup: adjustGroupData,
    				affectRoomType: true,
    				signUpRoomList: signUpRooms
    		};
    		aw.ajax({
    			url: "api/nonmembergroup/nonmemsaveadjust",
    			data:aw.customParam(saveData),
    			dataType: "json",
    			success: function(datas){
    				Dialog.alert({
        				tip: "提示！",
        				content: "保存成功！"
        			});
    				widget.get("grid").refresh();
    				widget.toGrid();
    			}
    		})
    	}	
    };
});
